package com.zzr.demo.redis.controller;

import com.zzr.demo.redis.service.LogService;
import com.zzr.demo.redis.service.SessionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: xiaomi
 * @date: 2021/10/8
 * @description:
 */
@RestController
@RequestMapping("session/manager")
@Api(tags = "会话管理：判断用户是否登录，是否过期")
@Slf4j
public class SessionManagerController {
    @Autowired
    SessionService sessionService;

    @GetMapping("/log/{userId}")
    @ApiOperation("用户登录")
    public String log(@PathVariable("userId") String userId) {
        String token = sessionService.createSession(userId);
        return token;
    }

    @GetMapping("/visit/{userId}/{token}")
    @ApiOperation("用户访问资源")
    public String visit(@PathVariable("userId") String userId, @PathVariable("token") String token) {
        String res = sessionService.validate(userId, token);
        return res;
    }

    @GetMapping("/logout/{userId}")
    @ApiOperation("用户登出")
    public boolean logout(@PathVariable("userId") String userId) {
        sessionService.destroy(userId);
        return true;
    }
}
